<!DOCTYPE html>
<html lang="cs">
<head>
<title>Hodina 3 - php</title>
<meta charset="utf-8">
<style>
 .red{color:red;}
 .yel{color:orange;}
 .grn{color:green;}
 table
{
border-collapse:collapse;
}
  table, td, th
{border:1px solid blue;
}
th
{
background-color:green;
color:white;
}
  
</style>
</head>
<body>
<?php
if (!empty($_GET)){
	$err = array();
	
	    	
	if (!is_numeric($_GET['height']) || $_GET['height']<0){
		$err['height'] = 'Špatná výška.';
	}
	    	
	if (!is_string($_GET['name'])||count($_GET['name'])<1){
		$err['name'] = 'Špatné jméno.';
	}
	    	
	if (!is_numeric($_GET['weight']) || $_GET['weight']<=0){
		$err['weight'] = 'Špatná váha.';
	}
	
}

$showForm = true;


if (isset($err)){
	if (!empty($err)){
		$showForm = true;
	
		foreach ($err as $current){
			echo "<div style='background-color: darkred;font-size:2em;'>  " . $current . "</div><br/>";
		}
	}
	else{
	    	$bmi = $_GET['weight'] /($_GET['height'] * $_GET['height']/10000);
	}
	$showForm = false;
}

?>

<!-- Formulář - zobrazí se pouze když PHP proměnná $showForm==true -->

<?php
if ($showForm){
?>

<form action="form.php" method="get">
		
	<br/><br/>
	<label for="name">Jmeno</label>
	<input type="text" id="name" name="name" value="<?php echo $_GET['name'];?>">

	<br/><br/>
	<label for="weight"> Váha <br/>(v kilogramech)</label>
	<input type="text" id="weight" name="weight" value="<?php echo $_GET['weight'];?>">
    
	<br/><br/>
	<label for="height"> Výška<br/>(v centimetrech)</label>
	<input type="text" id="height" name="height" value="<?php echo $_GET['height'];?>">

	<input type="submit" value="Vypočítat">
</form>
<?php
}else{

if(empty($err)){
echo "<br><table><tr><th colspan=\"3\"><b>".$_GET['name']."</b></th></tr>";
echo "<tr><td>".Váha."</td><td>".$_GET['weight']."</td><td>".kg."</td></tr>";
echo "<tr><td>".Výška."</td><td>".$_GET['height']."</td><td>".cm."</td></tr>";
echo "<tr><td>".BMI."</td><td colspan=\"2\">".number_format( $bmi, 2)."</td></tr>";
echo "<tr><td  colspan=\"3\" " ;
if($bmi>25||$bmi<18.5){
if($bmi>30){echo "class=\"red\">Obezita";}
else if($bmi<16.5){echo "class=\"red\">Podvýživa";}
else if($bmi<18.5){echo "class=\"yel\">Podváha";}
else if($bmi>25){echo "class=\"yel\">Nadváha";}

}
else{echo "class=\"grn\">Ideální váha";}


echo"</td></tr></table>";
}
} 
?>
</body>
</html> 